home *** CD-ROM | disk | FTP | other *** search
- Path: ub239.dialup.uwa.edu.au!not-for-mail
- From: prye@cyllene.uwa.edu.au (Peter Rye)
- Newsgroups: comp.lang.c
- Subject: Re: What is wrong in this code?
- Date: 13 Jan 1996 14:54:55 +0800
- Organization: The University of Western Australia
- Message-ID: <4d7kvv$j8@ub239.dialup.uwa.edu.au>
- References: <DL2z7o.2K5@scisun.sci.ccny.cuny.edu>
- NNTP-Posting-Host: ub239.dialup.uwa.edu.au
-
- sergio@sci.ccny.cuny.edu (Sergio Rojas) writes:
-
- Hi Sergio,
-
- You have demonstrated a couple of common problems here.
- Possibly more...I'm sure we'll both find out soon enough.
-
-
- >#include <stdio.h>
- >#include <math.h>
- >main()
- >{
- >double x;
-
- >printf("\n Enter a number \n");
-
- >scanf("%f", &x);
- ^-------------- You have told the compiler a lie.
- &x is the address of a double and
- you've told it to expect a float.
- Should be "%lf"...
-
- >printf("\n %e to the power %e is equal to ", x , x );
-
- >x = pow(x,x);
- ^-------------------- This line is a problem.
- I believe this will give rise to
- "undefined behavior" because you
- are modifying x using a function
- which takes x as an argument.
- Use something like y = pow(x,x);
- Where y is declared as a double,
- then use y in the printf below.
-
- >printf(" %12.6f \n", x );
-
- >}
-
- >Some output are as follows:
-
- > Enter a number
- >2
-
- > 2.000000e+00 to the power 2.000000e+00 is equal to 4.000000
- >scisun{sergio}121% a.out
-
- > Enter a number
- >3
-
- > 3.200000e+01 to the power 3.200000e+01 is equal to 1461501637330902918203684832716283019655932542976.000000
-
-
- > Thanks for your comments,
-
- >rojas
- >Email: sergio@scisun.sci.ccny.cuny.edu
-
- --
- | Peter Rye |
- | Respiratory Research Fellow |
- | Princess Margaret Hospital for Children |
- | Perth, Western Australia |
-